import Taro, { Component, Config } from '@tarojs/taro'
import Index from './pages/tabbar/homeC/index'
import './app.scss'
import { webLogIn } from "@/api/channelApi"
import { isWeChat } from '@/utils/utils'
import {AUTH_TOKEN, SOURCE_ID, USER_INFO} from '@/config/storage'
import { getMiniAppConfig, getCommonConfig } from "@/api/systemApi"
import {
  setAppConfig,
  setAppletConfig,
  setSessionStorage,
  getBrand,
  getUserTokenStorage,
  setUserTokenStorage
} from '@/utils/storage'
import { toLogin, toWeChatLogin } from "@/api/oauthApi"
import { getJsSdk } from '@/api/oauthApi'
import {codeToUser, weChartLogin} from "@/api/oauthApi";

class App extends Component {
  globalData: any
  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  config: Config = {
    pages: [
      'pages/tabbar/home/index',
      'pages/tabbar/cart/index',
      'pages/tabbar/user/index',
      'pages/tabbar/category/index',
      'pages/tabbar/comment/index',
      'pages/tabbar/popular/index',
      // 'pages/tabbar/integral/index',

      //  商品搜索
      'pages/list/custom-list/index',

      //  商品搜索
      'pages/search/index',
      //  订单评论列表
      'pages/comment/list/index',
      //  评价详情
      'pages/comment/detail/index',
      //  商品评价
      'pages/comment/goods/index',
      //  用户登录
      'pages/login/index',

      //  计算订单
      'pages/order/calc/index',
      //  订单列表
      'pages/order/list/index',
      //  订单支付
      'pages/order/unpaid/index',
      //  订单支付后跳转
      'pages/order/detail/index',
      //  订单信息
      'pages/order/info/index',
      //  订单物流
      'pages/order/logistics/index',
      //  订单核销码
      'pages/order/verification/index',
      //  订单评价
      'pages/order/comment/index',

      //  实物商品信息
      'pages/goods/info/real/index',
      'pages/real/info/index',

      //  开通会员
      'pages/member/open/index',
      //  会员续费
      'pages/member/pay/index',
      //  会员收藏
      // 'pages/member/favorite/index',
      //  地址列表
      'pages/address/list/index',
      //  地址信息
      'pages/address/info/index',
      //  关于我们
      'pages/about/index',
      //  用户设置
      'pages/setting/index',
      //  我的团队
      'pages/user/team/index',

      //  资讯列表
      'pages/news/list/index',
      //  资讯详情
      'pages/news/info/index',
      //  富文本详情
      'pages/news/blank/index',


      //  web 浏览器
      'pages/web/index',

      //  系统信息
      'pages/system/info/index',

      // 投诉页面
      'pages/service/complaint-index',
      // 联系客服
      'pages/service/index',

      //  自定义页面
      'pages/custom/page/index',
      // 支付空白
      'pages/order/pay-text/index',


      //  测试页面
      'pages/test/index',


      '/pages/auth/index',
      '/pages/cty-goods/index',
      '/pages/bindingmobile/index',
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarTextStyle: 'black',
      navigationBarTitleText: '迅蜂商城',
      navigationBarBackgroundColor: '#fff',
    },
    tabBar: {
      custom: true,
      list: [
        {
          pagePath: "pages/tabbar/home/index",
          text: "  "
        }, {
          pagePath: "pages/tabbar/category/index",
          text: "  "
        }, {
        //   pagePath: "pages/tabbar/cart/index",
        //   text: "  "
        // }, {
          pagePath: "pages/tabbar/comment/index",
          text: "  "
        }, {
          pagePath: "pages/tabbar/user/index",
          text: "  "
        }
      ],
      color: '#333',
      selectedColor: '#333',
      backgroundColor: '#fff',
      borderStyle: 'black'
    },
    subPackages: [
      {
        root: 'market/',
        pages: [
          //  团购列表
          'pages/group/list/index',
          //  团购商品
          'pages/group/goods/index',
          //  待拼团
          'pages/group/wait/index',

          //  秒杀列表
          'pages/seckill/list/index',
          //  秒杀商品详情
          'pages/seckill/goods/index',

          'pages/wallet/home/index',
          'pages/wallet/record/index',
          'pages/wallet/recharge/index',
          // 'pages/integral/mall/index',
          'pages/integral/home/index',
          'pages/integral/checkin/index',
          'pages/integral/record/index',
          'pages/integral/goods/index',
          'pages/integral/order/add/index',
          'pages/integral/order/list/index',
          'pages/integral/order/info/index',
          'pages/coupon/home/index',
          'pages/coupon/receive/index',
        ]
      }
    ],
    preloadRule: {
      "pages/tabbar/home/index": {
        "network": "all",
        "packages": [
          "market/"
        ]
      }
    },
    permission: {
      "scope.userLocation": {
        "desc": "您的位置信息将有利于我们更好的为您服务"
      }
    }
  }

  constructor() {
    super(...arguments)
    this.globalData = {
      appConfig: {},
      subscribe: {}
    }
  }

  async componentWillMount() {
    setUserTokenStorage(Taro.getStorageSync(AUTH_TOKEN))
    //  写入小程序用户分享参数
    // const { params: { sourceId, code } }:any = this.$router
    // sourceId && Taro.setStorageSync(SOURCE_ID, sourceId)
    const { params: { s, code } }:any = this.$router
    s && Taro.setStorageSync(SOURCE_ID, s)
    if (isWeChat() && !Taro.getStorageSync('AUTH_ORIGINAL') && !code) {
      toWeChatLogin()
      // code ? this.codeToUsers(code) : toWeChatLogin()
      return false
    }

    // console.log(isWeChat())
    // !isWeChat() && this.getUserInfo()
    // isWeChat() && this.getUserInfo()
    await this.getConfig()
  }

  //  获取小程序配置
  async getConfig() {
    await this.getUserInfo()
    //  拉取小程序配置
    await getCommonConfig('index_app_config,pink_config,freight').then(({ data }) => {
      document.title = '迅蜂商城';
      setAppletConfig(data)
    })

    await getMiniAppConfig().then(({ data }) => {
      Object.keys(data).map(name => {
        if (data[name]['menuName']) {
          data[name] = data[name]['value']
        } else {
          data[name] = data[name]['items']
        }
      })
      setAppConfig(data)
    })
  }

  //  获取用户信息
  async getUserInfo() {
    const sourceId = Taro.getStorageSync(SOURCE_ID)
    const openId = Taro.getStorageSync('OPEN_ID')
    await webLogIn({ sourceId, openId }).then(({ data }) => {
      setUserTokenStorage(data.token)
      setSessionStorage('AUTH_TOKEN_BRAND', data.channel.brand)
      Taro.setStorageSync(AUTH_TOKEN, data.token)
      Taro.setStorageSync(USER_INFO, data.userInfo)
      // console.log(Taro.getStorageSync(USER_INFO))
    })
  }

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Index/>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))
